package com.project.service;

import com.project.entity.SchoolManagerInfo;
import com.project.entity.User;
import com.project.myBeans.SchoolManagerDetail;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//让checked例外也回滚
@Transactional(rollbackFor = Exception.class)
@Service
public class SchoolMangerServiceImpl extends BaseService implements SchoolMangerService {
    @Autowired
    private UserService userService;
    @Autowired
    private SchoolManagerInfoService schoolManagerInfoService;

    /**
     * author: WheelChen
     * date: 2017/8/8
     * function: 创建学院管理员账号
     *
     * @param schoolManagerDetail user表 + schoolManagerInfo表
     * @return
     */
    @Override
    public boolean addAccount(SchoolManagerDetail schoolManagerDetail) throws Exception {
        boolean flag = false;
        User user = schoolManagerDetail.getUser();
        SchoolManagerInfo schoolManagerInfo = schoolManagerDetail.getSchoolManagerInfo();
        userService.createUser(user);
        if (user.getId() != null){
            schoolManagerInfo.setUserid(user.getId());
            schoolManagerInfoService.addOne(schoolManagerInfo);
            flag = true;
        }
        return flag;
    }

    /**
     * author: WheelChen
     * date: 2017/10/10
     * function: 根据学院id查找学院管理员用户表信息
     *
     * @param schoolId
     * @return
     */
    @Override
    public User findOneBySchoolId(Integer schoolId) throws Exception {
        SchoolManagerInfo schoolManagerInfo = schoolManagerInfoService.findOneBySchoolId(schoolId);
        if (schoolManagerInfo == null)
            return null;
        return userService.findOneByUserID(schoolManagerInfo.getUserid());
    }
}
